//
//  UserModel.swift
//  LocationTrackerAdmin
//
//  Created by mac on 2025/2/24.
//  用户模型

import Vapor
import Fluent
import SQLKit
import FluentSQL

final class UserModel: Model, Content {
    
    static var schema: String {
        return "users"
    }
    
    @ID(custom: UserModel.id)
    var id: String? //用户ID，主键
    
    @Field(key: UserModel.name)
    var name: String
    
    @Field(key: UserModel.avatar)
    var avatar: String
    
    @Field(key: UserModel.phone)
    var phone: String
    
    @Field(key: UserModel.deviceID)
    var deviceID: String?
    
    @Field(key: UserModel.language)
    var language: String
    
    @Field(key: UserModel.latitude)
    var latitude: Double?
    
    @Field(key: UserModel.longitude)
    var longitude: Double?
    
    @Field(key: UserModel.detail)
    var detail: String?
    
    @Field(key: UserModel.createDate)
    var createDate: Date
    
    @Field(key: UserModel.loginDate)
    var loginDate: Date
    
    @Field(key: UserModel.exitDate)
    var exitDate: Date?
    
    @Field(key: UserModel.locationUpdateDate)
    var locationUpdateDate: Date?
    
    @Field(key: UserModel.online)
    var online: Bool
    
    @Field(key: UserModel.sharing)
    var sharing: Bool
    
    @Field(key: UserModel.isVIP)
    var isVIP: Bool
    
    @Field(key: UserModel.vipExpireDate)
    var vipExpireDate: Date?
    
    @Field(key: UserModel.locationPermissions)
    var locationPermissions: Int
    
    init() {
        
    }
    
    init(id: String? = nil, name: String, avatar: String, phone: String, deviceID: String?, language: String, latitude: Double? = nil, longitude: Double? = nil, detail: String?, createDate: Date, loginDate: Date, exitDate: Date? = nil, locationUpdateDate: Date? = nil, online: Bool, sharing: Bool, isVIP: Bool = false, vipExpireDate: Date? = nil, locationPermissions: Int = 0) {
        self.id = id
        self.name = name
        self.avatar = avatar
        self.phone = phone
        self.deviceID = deviceID
        self.language = language
        self.latitude = latitude
        self.longitude = longitude
        self.detail = detail
        self.createDate = createDate
        self.loginDate = loginDate
        self.exitDate = exitDate
        self.locationUpdateDate = locationUpdateDate
        self.online = online
        self.sharing = sharing
        self.isVIP = isVIP
        self.vipExpireDate = vipExpireDate
        self.locationPermissions = locationPermissions
    }
    
}

extension UserModel {
    static let id: FieldKey = FieldKey(stringLiteral: "user_id")
    static let name: FieldKey = FieldKey(stringLiteral: "user_account")
    static let phone: FieldKey = FieldKey(stringLiteral: "user_phone")
    static let avatar: FieldKey = FieldKey(stringLiteral: "user_avatar")
    static let deviceID: FieldKey = FieldKey(stringLiteral: "user_device_id")
    static let language: FieldKey = FieldKey(stringLiteral: "user_language")
    static let latitude: FieldKey = FieldKey(stringLiteral: "user_latitude")
    static let longitude: FieldKey = FieldKey(stringLiteral: "user_longitude")
    static let detail: FieldKey = FieldKey(stringLiteral: "user_detail")
    static let createDate: FieldKey = FieldKey(stringLiteral: "user_create_date")
    static let loginDate: FieldKey = FieldKey(stringLiteral: "user_login_date")
    static let exitDate: FieldKey = FieldKey(stringLiteral: "user_exit_date")
    static let locationUpdateDate = FieldKey(stringLiteral: "user_location_update_date")
    static let online = FieldKey(stringLiteral: "user_online")
    static let sharing = FieldKey(stringLiteral: "user_location_sharing")
    static let isVIP = FieldKey(stringLiteral: "user_is_vip")
    static let vipExpireDate = FieldKey(stringLiteral: "user_vip_expire_date")
    static let locationPermissions = FieldKey(stringLiteral: "user_location_permissions")
}

struct UserModelMigration: AsyncMigration {
    func prepare(on database: any Database) async throws {
        try await database.schema(UserModel.schema)
            .field(UserModel.id, .string, .custom("COMMENT '用户ID'"))
            .field(UserModel.phone, .string, .custom("COMMENT '用户手机号'"))
            .field(UserModel.name, .string, .required ,.custom("COMMENT '用户名称'"))
            .field(UserModel.avatar, .string, .sql(.default("")) ,.custom("COMMENT '用户头像'"))
            .field(UserModel.deviceID, .string ,.custom("COMMENT '设备ID'"))
            .field(UserModel.language, .string, .custom("COMMENT '语言'"))
            .field(UserModel.latitude, .double, .custom("COMMENT '用户最新纬度'"))
            .field(UserModel.longitude, .double, .custom("COMMENT '用户最新经度'"))
            .field(UserModel.detail, .string ,.custom("COMMENT '用户当前地址详情'"))
            .field(UserModel.createDate, .custom("datetime(0)"), .sql(.default(SQLFunction("now"))), .custom("COMMENT '用户创建日期'"))
            .field(UserModel.loginDate, .custom("datetime(0)"), .custom("COMMENT '用户登录日期'"))
            .field(UserModel.exitDate, .custom("datetime(0)"), .custom("COMMENT '用户退出日期'"))
            .field(UserModel.locationUpdateDate, .custom("datetime(0)"), .custom("COMMENT '用户最新位置的日期'"))
            .field(UserModel.online, .bool, .sql(.default(false)), .custom("COMMENT '用户是否在线'"))
            .field(UserModel.sharing, .bool, .sql(.default(true)), .custom("COMMENT '用户是否开启位置分享'"))
            .field(UserModel.isVIP, .bool, .sql(.default(false)), .custom("COMMENT '用户是否是VIP'"))
            .field(UserModel.vipExpireDate, .custom("datetime(0)"), .custom("COMMENT '用户会员过期时间'"))
            .field(UserModel.locationPermissions, .int, .sql(.default(0)), .custom("COMMENT '定位权限0未定义 1受限 2拒绝 3始终允许  4使用时'"))
            .create()
    }
    
    func revert(on database: any Database) async throws {
       try await database.schema(UserModel.schema)
            .delete()
    }
}
